package com.tacionery.android.gpi.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {
	private JSONParser jsonParser;
	
	private static String loginURL = "http://localhost/GuitarPedalsInventory_API/";
	private static String registerURL = "http://localhost/GuitarPedalsInventory_API/";
	
	private static String loginTag = "login";
	private static String registerTag = "register";
	
	public UserFunctions() {
		jsonParser = new JSONParser();
	}
	
	public JSONObject loginUser (String email, String passwd) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		
		params.add(new BasicNameValuePair("tag", loginTag));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("passwd", passwd));
		
		JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
		
		return json;
	}
	
	public JSONObject registerUser (String name, String email, String passwd) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		
        params.add(new BasicNameValuePair("tag", registerTag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("passwd", passwd));
        
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        
        return json;
	}
	
	public boolean isUserLoggedIn (Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCont();
		
		if (count > 0) {
			return true;
		}
		
		return false;
	}
	
	public boolean logoutUser (Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}
}





















